import { _decorator, Node, ParticleSystem2D, tween, Vec3 } from 'cc';
import { BaseEffect } from './BaseEffect';

const { ccclass, property } = _decorator;

@ccclass('DashEffect')
export class DashEffect extends BaseEffect {
    @property(ParticleSystem2D)
    private particleSystem: ParticleSystem2D | null = null;

    @property
    private trailDuration: number = 0.5;

    @property(Node)
    private trailNode: Node | null = null;

    private originalScale: Vec3 = new Vec3();
    private trailTween: any = null;

    start() {
        super.start();
        if (this.trailNode) {
            this.originalScale = this.trailNode.scale.clone();
            this.trailNode.active = false;
        }
    }

    protected onEffectStart(): void {
        // 启动粒子系统
        if (this.particleSystem) {
            this.particleSystem.resetSystem();
            this.particleSystem.enabled = true;
        }

        // 显示拖尾效果
        if (this.trailNode) {
            this.trailNode.active = true;
            this.trailNode.scale = this.originalScale.clone();

            // 创建拖尾缩放动画
            this.trailTween = tween(this.trailNode)
                .to(this.trailDuration, { scale: new Vec3(0, this.originalScale.y, 1) })
                .start();
        }
    }

    protected onEffectEnd(): void {
        // 停止粒子系统
        if (this.particleSystem) {
            this.particleSystem.enabled = false;
        }

        // 停止拖尾动画
        if (this.trailTween) {
            this.trailTween.stop();
        }

        // 隐藏拖尾
        if (this.trailNode) {
            this.trailNode.active = false;
        }
    }
} 